<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">

      <el-form-item label="基金用户" prop="userId">
        <!--        <el-input v-model="form.userId" />-->
        <el-select v-model="form.userId" placeholder="请选择用户" value-key="id" style="width: 120px">
          <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="基金代码" prop="fundCode">
        <!--        <el-input v-model="form.fundCode" />-->
        <el-select v-model="form.fundCode" placeholder="请选择基金" value-key="fundCode" style="width: 300px">
          <el-option v-for="item in fundList" :key="item.id" :label="'('+item.fundCode+') '+item.fundName" :value="item.fundCode" />
        </el-select>
      </el-form-item>

      <el-form-item label="涨幅提醒值" prop="increaseReminder">
        <el-input-number v-model.number="form.increaseReminder" size="mini" :min="-10" :max="10" />
      </el-form-item>
      <el-form-item label="跌幅提醒值" prop="fallReminder">
        <el-input-number v-model.number="form.fallReminder" size="mini" :min="-10" :max="10" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { add } from '@/api/fundUserRelation'
import { getAllUser } from '@/api/fundUser'
import { getAllFund } from '@/api/fundSource'

export default {
  data() {
    return {
      form: {
        userId: null,
        fundCode: null,
        increaseReminder: 3,
        fallReminder: 3
      },
      userList: null,
      fundList: null
    }
  },
  created() {
    this.fetchUserList()
    this.fetchUserList()
    this.fetchFundList()
  },
  methods: {
    fetchFundList() {
      var vm = this
      getAllFund().then(response => {
        vm.fundList = response.data
        console.log(vm.fundList)
      })
    },
    fetchUserList() {
      var vm = this
      getAllUser().then(response => {
        vm.userList = response.data
        console.log(vm.userList)
      })
    },
    onSubmit() {
      var vm = this
      add(vm.form).then(response => {
        var msg = response.message
        vm.$message.success(msg)
        this.$router.go(-1)
      })
    },
    onCancel() {
      // this.$store.dispatch('delVisitedViews', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>

